
#include "mainwindow.h"

#include <QComboBox>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QGroupBox>
#include <QGridLayout>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QVBoxLayout>

#include <QListWidget>
#include <QScrollArea>

#include <QtPrivateDeclar.h>
#include <NetworkRequest.h>

#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonValue>

#include <QTableWidget>


MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
{
    QVBoxLayout *layout = new QVBoxLayout(this);


    QPushButton *button = new QPushButton;
    QLineEdit *lineEdit = new QLineEdit;
    QListWidget *listwidget = new QListWidget();
    QTableWidget *tableWidget = new QTableWidget;
    // tableWidget->setColumnCount(5);
    tableWidget->setColumnCount(5);
    tableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem("title"));
    tableWidget->setHorizontalHeaderItem(1, new QTableWidgetItem("category"));
    tableWidget->setHorizontalHeaderItem(2, new QTableWidgetItem("tags"));
    tableWidget->setHorizontalHeaderItem(3, new QTableWidgetItem("url"));
    tableWidget->setHorizontalHeaderItem(4, new QTableWidgetItem("date"));

// 1.1
    QGroupBox *searchBox = new QGroupBox("搜索设置");
    QVBoxLayout *searchBoxLayout = new QVBoxLayout(searchBox);
    searchBoxLayout->addWidget(lineEdit);
    
    layout->addWidget(searchBox);
    // layout->addWidget(listwidget);
    layout->addWidget(tableWidget);
    // resize(1200, 800);

    NetworkRequest *request = new NetworkRequest;

    connect(request, &NetworkRequest::replyRequest, [=](const QString &content){
        QJsonDocument document = QJsonDocument::fromJson(content.toLocal8Bit());
        tableWidget->setRowCount(document.array().size());

        int row = 0;
        for (auto item : document.array())
        {
            QJsonObject object = item.toObject();
            QtPrivateDeclar obj;
            
            obj.setTitle(object.value("title").toString());
            obj.setCategory(object.value("category").toString());
            obj.setTags(object.value("tags").toString());
            obj.setUrl(object.value("url").toString());
            obj.setDate(object.value("date").toString());

            listwidget->addItem(obj.getTitle());

// -- 首次使用 QTableWidgetItem
            // QTableWidgetItem *itemTitle = new QTableWidgetItem(obj.getTitle());
            // QTableWidgetItem *itemCategory = new QTableWidgetItem(obj.getCategory());
            // QTableWidgetItem *itemTags = new QTableWidgetItem(obj.getTags());
            // QTableWidgetItem *itemUrl = new QTableWidgetItem(obj.getUrl());
            // QTableWidgetItem *itemDate = new QTableWidgetItem(obj.getDate());

            // itemTitle->setText(obj.getTitle());
            // itemCategory->setText(obj.getCategory());
            // itemTags->setText(obj.getTags());
            // itemUrl->setText(obj.getUrl());
            // itemDate->setText(obj.getDate());

            // tableWidget->setItem(row, 0, itemTitle);
            // tableWidget->setItem(row, 1, itemCategory);
            // tableWidget->setItem(row, 2, itemTags);
            // tableWidget->setItem(row, 3, itemUrl);
            // tableWidget->setItem(row, 4, itemDate);
// -- 第二次
            // QTableWidgetItem *itemTitle = new QTableWidgetItem(obj.getTitle());
            // QTableWidgetItem *itemCategory = new QTableWidgetItem(obj.getCategory());
            // QTableWidgetItem *itemTags = new QTableWidgetItem(obj.getTags());
            // QTableWidgetItem *itemUrl = new QTableWidgetItem(obj.getUrl());
            // QTableWidgetItem *itemDate = new QTableWidgetItem(obj.getDate());

// -- 最终
            tableWidget->setItem(row, 0, new QTableWidgetItem(obj.getTitle()));
            tableWidget->setItem(row, 1, new QTableWidgetItem(obj.getCategory()));
            tableWidget->setItem(row, 2, new QTableWidgetItem(obj.getTags()));
            tableWidget->setItem(row, 3, new QTableWidgetItem(obj.getUrl()));
            tableWidget->setItem(row, 4, new QTableWidgetItem(obj.getDate()));
            row++;
        }
        tableWidget->resizeColumnsToContents();
        tableWidget->setSortingEnabled(true);
    });
    request->requestString("https://appimage.github.io/search.json");

    connect(lineEdit, &QLineEdit::textChanged, [=](const QString &text){
        for (int row = 0; row < tableWidget->rowCount(); row++)
        {
            QTableWidgetItem *item = tableWidget->item(row, 0);
            if (item->text().contains(text, Qt::CaseInsensitive) == 0) {
                tableWidget->hideRow(row);
            } else {
                tableWidget->showRow(row);
            }
        }
    });
    
    resize(1200, 800);

}
MainWindow::~MainWindow()
{
	
}
